﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;

using Mvc.XForms.Controls.Settings;
using Mvc.XForms.Utilities;

namespace Mvc.XForms.Controls {
	public class TextArea<TContext> : CoreControlBase<TContext>, ITextArea {
		private Expression<Func<TContext, object>> bind;
		private const int rowsDefault = 2;
		private const int columnsDefault = 20;

		public TextAreaSettings Settings { get; private set; }
		public string EncodedValue { get; set; }
		public int Rows { get; set; }
		public int Columns { get; set; }

		public override XFormControlType ControlType {
			get { return XFormControlType.TextArea; }
		}

		public TextArea(IContainerControl<TContext> parent, Expression<Func<TContext, object>> bind)
			: base(parent, bind.Body) {
			Settings = new TextAreaSettings(this);
			CssClass.Add("textarea");
			Rows = rowsDefault;
			Columns = columnsDefault;

			this.bind = bind;
		}

		protected override Func<object> SetValueAction {
			get { return () => bind.Compile()(Parent.ModelContext); }
		}

		public override string Render() {
			this.EncodedValue = this.Value == null ? string.Empty : HttpUtility.HtmlEncode(this.Value.ToString());

			var control = XForm.Formatter.TextArea(this);
			return control;
		}
	}
}